﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    class BizLopHocPhan_BangDiem
    {
        public static LopHocPhan_BangDiem GetByID(string maLopHocPhan, string maDotThi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.LopHocPhan_BangDiemDbSet.Where(e => e.MaDotThiGUID == maDotThi && e.MaLopHocPhanGUID == maLopHocPhan).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN UpdateBangDiem(string maLopHocPhan, string maDotThi, TRANGTHAI_BANGDIEM trangThaiBangDiem, string maNguoiNhapDiemHoacKiemTra)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var bangDiem = db.LopHocPhan_BangDiemDbSet.Where(e => e.MaDotThiGUID == maDotThi && e.MaLopHocPhanGUID == maLopHocPhan).FirstOrDefault();
                if (bangDiem == null)
                    return CODE_RESULT_RETURN.ThatBai;
                var nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<LopHocPhan_BangDiem>(nhatKy, bangDiem, bangDiem.MaBangDiemGUID, LOAIDOITUONG.BẢNG_ĐIỂM, LOAITHAOTAC.Chỉnh_sửa);
                if (trangThaiBangDiem == TRANGTHAI_BANGDIEM.DANHAPXONG)
                {
                    bangDiem.NgayNhapBangDiem = DateTime.Now;
                    bangDiem.MaNguoiDungNhapBangDiem = maNguoiNhapDiemHoacKiemTra;
                }
                else if (trangThaiBangDiem == TRANGTHAI_BANGDIEM.BAOLOINHAP || trangThaiBangDiem == TRANGTHAI_BANGDIEM.DAPHEDUYET)
                {
                    bangDiem.NgayKiemTraBangDiem = DateTime.Now;
                    bangDiem.MaNguoiDungKiemTraBangDiem = maNguoiNhapDiemHoacKiemTra;
                }
                bangDiem.TrangThai = (int)trangThaiBangDiem;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<LopHocPhan_BangDiem>(nhatKy, bangDiem);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }
}
